home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / File / CheckTree.pm < prev    next >
Text File  |  1995-03-20  |  4KB  |  114 lines

  1. package File::CheckTree;
  2. require 5.000;
  3. require Exporter;
  4.  
  5. @ISA = qw(Exporter);
  6. @EXPORT = qw(validate);
  7.  
  8. # $RCSfile: validate.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:19 $
  9.  
  10. # The validate routine takes a single multiline string consisting of
  11. # lines containing a filename plus a file test to try on it.  (The
  12. # file test may also be a 'cd', causing subsequent relative filenames
  13. # to be interpreted relative to that directory.)  After the file test
  14. # you may put '|| die' to make it a fatal error if the file test fails.
  15. # The default is '|| warn'.  The file test may optionally have a ! prepended
  16. # to test for the opposite condition.  If you do a cd and then list some
  17. # relative filenames, you may want to indent them slightly for readability.
  18. # If you supply your own "die" or "warn" message, you can use $file to
  19. # interpolate the filename.
  20.  
  21. # Filetests may be bunched:  -rwx tests for all of -r, -w and -x.
  22. # Only the first failed test of the bunch will produce a warning.
  23.  
  24. # The routine returns the number of warnings issued.
  25.  
  26. # Usage:
  27. #    use File::CheckTree;
  28. #    $warnings += validate('
  29. #    /vmunix            -e || die
  30. #    /boot            -e || die
  31. #    /bin            cd
  32. #        csh            -ex
  33. #        csh            !-ug
  34. #        sh            -ex
  35. #        sh            !-ug
  36. #    /usr            -d || warn "What happened to $file?¥n"
  37. #    ');
  38.  
  39. sub validate {
  40.     local($file,$test,$warnings,$oldwarnings);
  41.     foreach $check (split(/¥n/,$_[0])) {
  42.     next if $check =~ /^#/;
  43.     next if $check =~ /^$/;
  44.     ($file,$test) = split(' ',$check,2);
  45.     if ($test =~ s/^(!?-)(¥w{2,}¥b)/$1Z/) {
  46.         $testlist = $2;
  47.         @testlist = split(//,$testlist);
  48.     }
  49.     else {
  50.         @testlist = ('Z');
  51.     }
  52.     $oldwarnings = $warnings;
  53.     foreach $one (@testlist) {
  54.         $this = $test;
  55.         $this =~ s/(-¥w¥b)/$1 ¥$file/g;
  56.         $this =~ s/-Z/-$one/;
  57.         $this .= ' || warn' unless $this =~ /¥|¥|/;
  58.         $this =~ s/^(.*¥S)¥s*¥|¥|¥s*(die|warn)$/$1 || valmess('$2','$1')/;
  59.         $this =~ s/¥bcd¥b/chdir (¥$cwd = ¥$file)/g;
  60.         eval $this;
  61.         last if $warnings > $oldwarnings;
  62.     }
  63.     }
  64.     $warnings;
  65. }
  66.  
  67. sub valmess {
  68.     local($disposition,$this) = @_;
  69. #    $file = $cwd . '/' . $file unless $file =~ m|^/|;
  70.     $file = $cwd . ':' . $file unless $file =~ m|^[^:]|;
  71.     if ($this =~ /^(!?)-(¥w)¥s+¥$file¥s*$/) {
  72.     $neg = $1;
  73.     $tmp = $2;
  74.     $tmp eq 'r' && ($mess = "$file is not readable by uid $>.");
  75.     $tmp eq 'w' && ($mess = "$file is not writable by uid $>.");
  76.     $tmp eq 'x' && ($mess = "$file is not executable by uid $>.");
  77.     $tmp eq 'o' && ($mess = "$file is not owned by uid $>.");
  78.     $tmp eq 'R' && ($mess = "$file is not readable by you.");
  79.     $tmp eq 'W' && ($mess = "$file is not writable by you.");
  80.     $tmp eq 'X' && ($mess = "$file is not executable by you.");
  81.     $tmp eq 'O' && ($mess = "$file is not owned by you.");
  82.     $tmp eq 'e' && ($mess = "$file does not exist.");
  83.     $tmp eq 'z' && ($mess = "$file does not have zero size.");
  84.     $tmp eq 's' && ($mess = "$file does not have non-zero size.");
  85.     $tmp eq 'f' && ($mess = "$file is not a plain file.");
  86.     $tmp eq 'd' && ($mess = "$file is not a directory.");
  87.     $tmp eq 'l' && ($mess = "$file is not a symbolic link.");
  88.     $tmp eq 'p' && ($mess = "$file is not a named pipe (FIFO).");
  89.     $tmp eq 'S' && ($mess = "$file is not a socket.");
  90.     $tmp eq 'b' && ($mess = "$file is not a block special file.");
  91.     $tmp eq 'c' && ($mess = "$file is not a character special file.");
  92.     $tmp eq 'u' && ($mess = "$file does not have the setuid bit set.");
  93.     $tmp eq 'g' && ($mess = "$file does not have the setgid bit set.");
  94.     $tmp eq 'k' && ($mess = "$file does not have the sticky bit set.");
  95.     $tmp eq 'T' && ($mess = "$file is not a text file.");
  96.     $tmp eq 'B' && ($mess = "$file is not a binary file.");
  97.     if ($neg eq '!') {
  98.         $mess =~ s/ is not / should not be / ||
  99.         $mess =~ s/ does not / should not / ||
  100.         $mess =~ s/ not / /;
  101.     }
  102.     print stderr $mess,"¥n";
  103.     }
  104.     else {
  105.     $this =~ s/¥$file/'$file'/g;
  106.     print stderr "Can't do $this.¥n";
  107.     }
  108.     if ($disposition eq 'die') { exit 1; }
  109.     ++$warnings;
  110. }
  111.  
  112. 1;
  113.  
  114.